/**
  ******************************************************************************
  * File Name          : envirctrl.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include "tskcfg.h"
#include "network.h"
#include "route.h"
#include "cJSON.h"
#include "ctrl_common.h"
#include "storage.h"
#include "collector.h"
#include "dwlcd.h"

typedef struct
{
	cJSON *root;
  cJSON *params;
  cJSON *key;
	char split_buf[64];
}route_ec_down_static_type;
/* 本文件中调用的一些变量,为了减少栈的消耗,定义到函数外的全局变量,使用前手动清0 */
static route_ec_down_static_type ec_down_static;

/* cmd */
uint8_t Route_ec_down_mufn(const char *str, uint8_t len)
{
	uint8_t err = 0;
	int td;
  int act;
	
	memset(&ec_down_static, 0, sizeof(route_ec_down_static_type));
	
/* *** */ 	
  ec_down_static.root = cJSON_Parse(str);
  
  ec_down_static.params = cJSON_GetObjectItem(ec_down_static.root, "params");
	if(ec_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }

/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "mufn");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
	
	td = atoi(ec_down_static.key->valuestring);
	if((td < 1) || (td > CTR_FAN_CHANNEL_ALLNUM))
	{
		err = 3;
    goto fnout;
	}
	td -= 1;
	
	act = atoi(strchr(ec_down_static.key->valuestring, ',') +1);
	if((act != 0) && (act != 1))
	{
		err = 3;
		goto fnout;
	}
	
	
/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "zsb");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 4;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(ec_down_static.key->valuestring, NULL) != 0)
	{
		err = 4;
    goto fnout;
	}
	
#if (ROUTE_INTEGRATED > 0)
	CtrFan_SetActuator(act, td, 1);
	Ctrl_RefreshStateFN(0);
#endif
	
fnout:
  cJSON_Delete(ec_down_static.key);
  cJSON_Delete(ec_down_static.params);
  cJSON_Delete(ec_down_static.root);
  
  return err;
}

uint8_t Route_ec_down_musl(const char *str, uint8_t len)
{
	uint8_t err = 0;
	int td;
  int act;
	
	memset(&ec_down_static, 0, sizeof(route_ec_down_static_type));
	
/* *** */ 	
  ec_down_static.root = cJSON_Parse(str);
  
  ec_down_static.params = cJSON_GetObjectItem(ec_down_static.root, "params");
	if(ec_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }

/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "musl");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
	
	td = atoi(ec_down_static.key->valuestring);
	if((td < 1) || (td > CTR_SHL_CHANNEL_ALLNUM))
	{
		err = 3;
    goto fnout;
	}
	td -= 1;
	
	act = atoi(strchr(ec_down_static.key->valuestring, ',') +1);
	if((act != 0) && (act != 1))
	{
		err = 3;
		goto fnout;
	}
	
	
/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "zsb");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 4;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(ec_down_static.key->valuestring, NULL) != 0)
	{
		err = 4;
    goto fnout;
	}
	
#if (ROUTE_INTEGRATED > 0)
	CtrShl_SetActuator(act, td, 1);
	Ctrl_RefreshStateSL(0);
#endif
	
fnout:
  cJSON_Delete(ec_down_static.key);
  cJSON_Delete(ec_down_static.params);
  cJSON_Delete(ec_down_static.root);
  
  return err;
}

uint8_t Route_ec_down_munq(const char *str, uint8_t len)
{
	uint8_t err = 0;
	int td;
  int act;
	
	memset(&ec_down_static, 0, sizeof(route_ec_down_static_type));
	
/* *** */ 	
  ec_down_static.root = cJSON_Parse(str);
  
  ec_down_static.params = cJSON_GetObjectItem(ec_down_static.root, "params");
	if(ec_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }

/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "munq");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
	
	td = atoi(ec_down_static.key->valuestring);
	if((td < 1) || (td > CTR_SHL_CHANNEL_ALLNUM))
	{
		err = 3;
    goto fnout;
	}
	td -= 1;
	
	act = atoi(strchr(ec_down_static.key->valuestring, ',') +1);
	if((act != 0) && (act != 1))
	{
		err = 3;
		goto fnout;
	}
	
	
/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "zsb");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 4;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(ec_down_static.key->valuestring, NULL) != 0)
	{
		err = 4;
    goto fnout;
	}
	
#if (ROUTE_INTEGRATED > 0)
	CtrNuq_SetActuator(act, td, 1);
	Ctrl_RefreshStateNQ(0);
#endif
	
fnout:
  cJSON_Delete(ec_down_static.key);
  cJSON_Delete(ec_down_static.params);
  cJSON_Delete(ec_down_static.root);
  
  return err;
}

uint8_t Route_ec_down_muvfd(const char *str, uint8_t len)
{
	uint8_t err = 0;
	int td;
  int act;
	
	memset(&ec_down_static, 0, sizeof(route_ec_down_static_type));
	
/* *** */ 	
  ec_down_static.root = cJSON_Parse(str);
  
  ec_down_static.params = cJSON_GetObjectItem(ec_down_static.root, "params");
	if(ec_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }

/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "muvf");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
	
	td = atoi(ec_down_static.key->valuestring);
	if((td < 1) || (td > CTR_VFD_CHANNEL_ALLNUM))
	{
		err = 3;
    goto fnout;
	}
	td -= 1;
	
	act = atoi(strchr(ec_down_static.key->valuestring, ',') +1);
	if((act < 0) || (act > 10))
	{
		err = 3;
		goto fnout;
	}
	
	
/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "zsb");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 4;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(ec_down_static.key->valuestring, NULL) != 0)
	{
		err = 4;
    goto fnout;
	}
	
#if (ROUTE_INTEGRATED > 0)
	CtrVfd_Actuator(act, td, 1);
	Ctrl_RefreshStateVF(0);
#endif
	
fnout:
  cJSON_Delete(ec_down_static.key);
  cJSON_Delete(ec_down_static.params);
  cJSON_Delete(ec_down_static.root);
  
  return err;
}

/* request */
uint8_t Route_ec_down_allpara(const char *str, uint8_t len)
{
	uint8_t err = 0;
	
	memset(&ec_down_static, 0, sizeof(route_ec_down_static_type));
	
/* *** */ 	
  ec_down_static.root = cJSON_Parse(str);
  
  ec_down_static.params = cJSON_GetObjectItem(ec_down_static.root, "params");
	if(ec_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }
	

/* *** */  
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "zsb");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(ec_down_static.key->valuestring, NULL) != 0)
	{
		err = 3;
    goto fnout;
	}

#if (ROUTE_INTEGRATED > 0)
	Route_ec_tonet_md();
	Route_ec_tonet_collector();
	Route_ec_tonet_fnst();
	Route_ec_tonet_slst();
	Route_ec_tonet_nqst();
	Route_ec_tonet_vfst();
#else
#endif
	
fnout:
  cJSON_Delete(ec_down_static.key);
  cJSON_Delete(ec_down_static.params);
  cJSON_Delete(ec_down_static.root);
  
  return err;
}

uint8_t Route_ec_down_jtpara(const char *str, uint8_t len)
{
	uint8_t err = 0;
	
	memset(&ec_down_static, 0, sizeof(route_ec_down_static_type));
	
	
/* *** */ 	
  ec_down_static.root = cJSON_Parse(str);
  
  ec_down_static.params = cJSON_GetObjectItem(ec_down_static.root, "params");
	if(ec_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }
	

/* *** */  
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "zsb");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(ec_down_static.key->valuestring, NULL) != 0)
	{
		err = 3;
    goto fnout;
	}
	
#if (ROUTE_INTEGRATED > 0)	
	Route_ec_tonet_jt();
#endif
	
fnout:
  cJSON_Delete(ec_down_static.key);
  cJSON_Delete(ec_down_static.params);
  cJSON_Delete(ec_down_static.root);
  
  return err;
}

uint8_t Route_ec_down_dlpara(const char *str, uint8_t len)
{
	uint8_t err = 0;
	
	memset(&ec_down_static, 0, sizeof(route_ec_down_static_type));
	
/* *** */ 	
  ec_down_static.root = cJSON_Parse(str);
  
  ec_down_static.params = cJSON_GetObjectItem(ec_down_static.root, "params");
	if(ec_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }
	

/* *** */  
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "zsb");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(ec_down_static.key->valuestring, NULL) != 0)
	{
		err = 3;
    goto fnout;
	}


#if (ROUTE_INTEGRATED > 0)	
	Route_ec_tonet_dl1();
	Route_ec_tonet_dl2();
#endif
	
fnout:
  cJSON_Delete(ec_down_static.key);
  cJSON_Delete(ec_down_static.params);
  cJSON_Delete(ec_down_static.root);
  
  return err;
}

uint8_t Route_ec_down_skpara(const char *str, uint8_t len)
{
	uint8_t err = 0;
	
	memset(&ec_down_static, 0, sizeof(route_ec_down_static_type));
	
	
/* *** */ 	
  ec_down_static.root = cJSON_Parse(str);
  
  ec_down_static.params = cJSON_GetObjectItem(ec_down_static.root, "params");
	if(ec_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }
	

/* *** */  
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "zsb");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(ec_down_static.key->valuestring, NULL) != 0)
	{
		err = 3;
    goto fnout;
	}


#if (ROUTE_INTEGRATED > 0)	
	Route_ec_tonet_sk();
#endif
	
fnout:
  cJSON_Delete(ec_down_static.key);
  cJSON_Delete(ec_down_static.params);
  cJSON_Delete(ec_down_static.root);
  
  return err;
}

uint8_t Route_ec_down_tdpara(const char *str, uint8_t len)
{
	uint8_t err = 0;
	
	memset(&ec_down_static, 0, sizeof(route_ec_down_static_type));

	
/* *** */ 	
  ec_down_static.root = cJSON_Parse(str);
  
  ec_down_static.params = cJSON_GetObjectItem(ec_down_static.root, "params");
	if(ec_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }
	

/* *** */  
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "tdzsb");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(ec_down_static.key->valuestring, NULL) != 0)
	{
		err = 3;
    goto fnout;
	}

#if (ROUTE_INTEGRATED > 0)	
	Route_ec_tonet_td();
#endif
	
fnout:
  cJSON_Delete(ec_down_static.key);
  cJSON_Delete(ec_down_static.params);
  cJSON_Delete(ec_down_static.root);
  
  return err;
}


/* data */
uint8_t Route_ec_down_md(const char *str, uint8_t len)
{
  uint8_t err = 0;
  Ctrl_ParaMDType *md = NULL;
	Route_PayloadType *proute = NULL;
  
  proute = pvPortMalloc(sizeof(Route_PayloadType));
  if(proute == NULL)
  {
    return 1;
  }
  
	memset(&ec_down_static, 0, sizeof(route_ec_down_static_type));
  memset(proute, 0, sizeof(Route_PayloadType));
  
	md = (Ctrl_ParaMDType *)(proute->payload);
	
	
/* *** */ 	
  ec_down_static.root = cJSON_Parse(str);
  
  ec_down_static.params = cJSON_GetObjectItem(ec_down_static.root, "params");
	if(ec_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }
	

/* *** */  
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "zsb");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(ec_down_static.key->valuestring, NULL) != 0)
	{
		err = 3;
    goto fnout;
	}
	
/* *** */  	
  ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "md");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 4;
    goto fnout;
  }
  
  switch(ec_down_static.key->valuestring[0])
  {
    case '0':
      md->mode = CTRL_MODE_MU;
      break;
    
    case '1':
      md->mode = CTRL_MODE_ZN;
      break;
    
    case '2':
      md->mode = CTRL_MODE_JT;
      break;
    
    case '3':
      md->mode = CTRL_MODE_DL;
      break;
    
    case '4':
      md->mode = CTRL_MODE_SK;
      break;
    
    default:
      err = 5;
      goto fnout;
  }
  
	if(md->mode == CTRL_MODE_SK)
	{
		if(ec_down_static.key->valuestring[2] != '1')
		{
			md->fnmdsken = 0;
		}
		else
		{
			md->fnmdsken = 1;
		}
		
		if(ec_down_static.key->valuestring[4] != '1')
		{
			md->slmdsken = 0;
		}
		else
		{
			md->slmdsken = 1;
		}
		
		if(ec_down_static.key->valuestring[6] != '1')
		{
			md->nqmdsken = 0;
		}
		else
		{
			md->nqmdsken = 1;
		}
		
		if(ec_down_static.key->valuestring[8] != '1')
		{
			md->vfmdsken = 0;
		}
		else
		{
			md->vfmdsken = 1;
		}
	}
	else
	{
		md->fnmdsken = Ctrl_ParaMD.fnmdsken;
		md->slmdsken = Ctrl_ParaMD.slmdsken;
		md->nqmdsken = Ctrl_ParaMD.nqmdsken;
		md->vfmdsken = Ctrl_ParaMD.vfmdsken;
	}
  
#if (ROUTE_INTEGRATED > 0)
	Storage_SaveType sftype;
	
	if(memcmp(&Ctrl_ParaMD, md, sizeof(Ctrl_ParaMDType)) != 0)
	{
		if(Ctr_CheckMDPara(md) != 0)
		{
			err = 6;
			goto fnout;
		}
		
		Ctrl_ParaMD = *md;
		
		sftype = SFLASH_SAVE_CTRMD;
		if(Storage_qhd != NULL)
		{
			xQueueSend(Storage_qhd, &sftype, 0);
		}
		
		Route_ec_tonet_md();
    DW_Send_ec_mode();
    if(Ctrl_ParaMD.mode == CTRL_MODE_SK)
    {
      DW_Send_ec_para_sk();
    }
	}
#endif
	
fnout:
  cJSON_Delete(ec_down_static.key);
  cJSON_Delete(ec_down_static.params);
  cJSON_Delete(ec_down_static.root);
  
  vPortFree(proute);
  
  return err;
}

uint8_t Route_ec_down_jt(const char *str, uint8_t len)
{
	uint8_t err = 0;
  Ctrl_ParaJTType *jt = NULL;
  int value;
	Route_PayloadType *proute = NULL;
  
  proute = pvPortMalloc(sizeof(Route_PayloadType));
  if(proute == NULL)
  {
    return 1;
  }
  
	memset(&ec_down_static, 0, sizeof(route_ec_down_static_type));
  memset(proute, 0, sizeof(Route_PayloadType));
	
	jt = (Ctrl_ParaJTType *)(proute->payload);


/* *** */	
  ec_down_static.root = cJSON_Parse(str);
  
  ec_down_static.params = cJSON_GetObjectItem(ec_down_static.root, "params");
	if(ec_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }

	
/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "zsb");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(ec_down_static.key->valuestring, NULL) != 0)
	{
		err = 3;
    goto fnout;
	}

#if (ROUTE_INTEGRATED == 0)
	strncpy(pl->did, key->valuestring, ROUTE_DID_BUFLEN);
#endif


/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "jtnqfq");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 4;
    goto fnout;
  }
	
	if(ec_down_static.key->valuestring[0] == '0')
	{
		jt->nqfq = 0;
	}
	else if(ec_down_static.key->valuestring[0] == '1')
	{
		jt->nqfq = 1;
	}
	else
	{
		err = 4;
    goto fnout;
	}

/************/
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "jtnqlotp");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 5;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value <0) || ( value >990))
  {
    err = 5;
    goto fnout;
  }
	
	jt->nqlotp = value;

/************/
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "jtfnhin3");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 6;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value < 0) || (value > 254))
  {
    err = 6;
    goto fnout;
  }
	
	jt->fnhin3 = value;
	
/************/
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "jtnqhitp");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 7;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value <0) || (value >990))
  {
    err = 7;
    goto fnout;
  }
	
	jt->nqhitp = value;
	
/************/
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "jtfnsptp");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 8;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value < 0) || (value > 50))
  {
    err = 8;
    goto fnout;
  }
	
	jt->fnsptp = value;

/************/
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "jtfnlotp");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 9;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value <0) || (value >990))
  {
    err = 9;
    goto fnout;
  }
	
	jt->fnlotp = value;

/************/
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "jtslhitp");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 10;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value <0) || (value >990))
  {
    err = 10;
    goto fnout;
  }
	
	jt->slhitp = value;

/************/
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "jtfnrttp");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 11;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value <0) || (value >20))
  {
    err = 11;
    goto fnout;
  }
	
	jt->fnrttp = value;
	
/************/
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "jtfnhitp");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 12;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value <0) || (value >990))
  {
    err = 12;
    goto fnout;
  }
	
	jt->fnhitp = value;

/************/
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "jtsllotp");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 13;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value <0) || (value >990))
  {
    err = 13;
    goto fnout;
  }
	
	jt->sllotp = value;

/************/
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "jtslrttp");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 14;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value <0) || (value >20))
  {
    err = 14;
    goto fnout;
  }
	
	jt->slrttp = value;

/************/
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "jtnqrttp");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 15;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value <0) || (value >20))
  {
    err = 15;
    goto fnout;
  }
	
	jt->nqrttp = value;
  
  /* 取高温低温报警 */
  jt->bjdw = Ctrl_ParaJT.bjdw;
  jt->bjgw = Ctrl_ParaJT.bjgw;
  jt->xcfnins = Ctrl_ParaJT.xcfnins;
  
#if (ROUTE_INTEGRATED > 0)
	Storage_SaveType sftype;
	
	if(memcmp(&Ctrl_ParaJT, jt, sizeof(Ctrl_ParaJTType)) != 0)
	{
		if(Ctr_CheckJTPara(jt) != 0)
		{
			err = 16;
			goto fnout;
		}
		
		Ctrl_ParaJT = *jt;
		
		sftype = SFLASH_SAVE_CTRJT;
		if(Storage_qhd != NULL)
		{
			xQueueSend(Storage_qhd, &sftype, 0);
		}
		
		Route_ec_tonet_jt();
    DW_Send_ec_para_jt();
	}
#endif
	
fnout:
  cJSON_Delete(ec_down_static.key);
  cJSON_Delete(ec_down_static.params);
  cJSON_Delete(ec_down_static.root);
	
  vPortFree(proute);
  
  return err;
}

uint8_t Route_ec_down_gdbj(const char *str, uint8_t len)
{
  uint8_t err = 0;
  int16_t gw,dw;
  int value;

	memset(&ec_down_static, 0, sizeof(route_ec_down_static_type));
	
/* *** */	
  ec_down_static.root = cJSON_Parse(str);
  
  ec_down_static.params = cJSON_GetObjectItem(ec_down_static.root, "params");
	if(ec_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }

	
/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "zsb");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(ec_down_static.key->valuestring, NULL) != 0)
	{
		err = 3;
    goto fnout;
	}

#if (ROUTE_INTEGRATED == 0)
	strncpy(pl->did, key->valuestring, ROUTE_DID_BUFLEN);
#endif


/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "jtbjgw");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 4;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value <0) || ( value >990))
  {
    err = 4;
    goto fnout;
  }
	
	gw = value;

/************/
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "jtbjdw");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 5;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value <0) || ( value >990))
  {
    err = 5;
    goto fnout;
  }
	
	dw = value;

	Storage_SaveType sftype;
	
	if((gw != Ctrl_ParaJT.bjgw) || (dw != Ctrl_ParaJT.bjdw))
	{	
		Ctrl_ParaJT.bjdw = dw;
    Ctrl_ParaJT.bjgw = gw;
		
		sftype = SFLASH_SAVE_CTRJT;
		if(Storage_qhd != NULL)
		{
			xQueueSend(Storage_qhd, &sftype, 0);
		}
		
		Route_ec_tonet_jt();
    DW_Send_ec_para_jt();
	}
	
fnout:
  cJSON_Delete(ec_down_static.key);
  cJSON_Delete(ec_down_static.params);
  cJSON_Delete(ec_down_static.root);
	
  return err;
}

uint8_t Route_ec_down_xckhd(const char *str, uint8_t len)
{
  uint8_t err = 0;
  uint16_t xckhd;
  int value;

	memset(&ec_down_static, 0, sizeof(route_ec_down_static_type));
	
/* *** */	
  ec_down_static.root = cJSON_Parse(str);
  
  ec_down_static.params = cJSON_GetObjectItem(ec_down_static.root, "params");
	if(ec_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }

	
/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "zsb");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(ec_down_static.key->valuestring, NULL) != 0)
	{
		err = 3;
    goto fnout;
	}

#if (ROUTE_INTEGRATED == 0)
	strncpy(pl->did, key->valuestring, ROUTE_DID_BUFLEN);
#endif


/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "khd");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 4;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value <0) || ( value >100))
  {
    err = 4;
    goto fnout;
  }
	
	xckhd = value;

	Storage_SaveType sftype;
	
	if(xckhd != Ctrl_ParaJT.xcfnins)
	{	
		Ctrl_ParaJT.xcfnins = xckhd;
		
		sftype = SFLASH_SAVE_CTRJT;
		if(Storage_qhd != NULL)
		{
			xQueueSend(Storage_qhd, &sftype, 0);
		}
		
		Route_ec_tonet_jt();
	}
	
fnout:
  cJSON_Delete(ec_down_static.key);
  cJSON_Delete(ec_down_static.params);
  cJSON_Delete(ec_down_static.root);
	
  return err;
}

uint8_t Route_ec_down_dl1(const char *str, uint8_t len)
{
	uint8_t err = 0;
  Ctrl_ParaDL1Type *dl1 = NULL;
  int value;
	uint8_t i;
	char *save = NULL;
	Route_PayloadType *proute = NULL;
  
  proute = pvPortMalloc(sizeof(Route_PayloadType));
  if(proute == NULL)
  {
    return 1;
  }
  
	memset(&ec_down_static, 0, sizeof(route_ec_down_static_type));
  memset(proute, 0, sizeof(Route_PayloadType));
	
	dl1 = (Ctrl_ParaDL1Type *)(proute->payload);
	
	
/* *** */ 	
  ec_down_static.root = cJSON_Parse(str);
  
  ec_down_static.params = cJSON_GetObjectItem(ec_down_static.root, "params");
	if(ec_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }
	

/* *** */  
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "zsb");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(ec_down_static.key->valuestring, NULL) != 0)
	{
		err = 3;
    goto fnout;
	}

#if (ROUTE_INTEGRATED == 0)
	strncpy(pl->did, key->valuestring, ROUTE_DID_BUFLEN);
#endif
	
	
/************/
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "dlsltp");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 4;
    goto fnout;
  }
	
	strncpy(ec_down_static.split_buf, ec_down_static.key->valuestring, 64);
	
	value = atoi(_strtok_r(ec_down_static.split_buf, "-", &save));
	if((value <0) || (value >990))
	{
		err = 4;
		goto fnout;
	}
	
	dl1->sltp[0] = value;
	
	for(i=1; i<CTR_SHL_CHANNEL_ALLNUM; i++)
	{
		value = atoi(_strtok_r(NULL, "-", &save));
		if((value <0) || (value >990))
		{
			err = 4;
			goto fnout;
		}
		
		dl1->sltp[i] = value;
	}

	
/************/
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "dlvftp");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 5;
    goto fnout;
  }
	
	strncpy(ec_down_static.split_buf, ec_down_static.key->valuestring, 64);
	
	value = atoi(_strtok_r(ec_down_static.split_buf, "-", &save));
	if((value <0) || (value >990))
	{
		err = 5;
		goto fnout;
	}
	
	dl1->vftp[0] = value;
	
	for(i=1; i<CTR_VFD_CHANNEL_ALLNUM; i++)
	{
		value = atoi(_strtok_r(NULL, "-", &save));
		if((value <0) || (value >990))
		{
			err = 5;
			goto fnout;
		}
		
		dl1->vftp[i] = value;
	}
	
	
/************/
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "dlfntp");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 6;
    goto fnout;
  }
	
	strncpy(ec_down_static.split_buf, ec_down_static.key->valuestring, 64);
	
	value = atoi(_strtok_r(ec_down_static.split_buf, "-", &save));
	if((value <0) || (value >990))
	{
		err = 6;
		goto fnout;
	}
	
	dl1->fntp[0] = value;
	
	for(i=1; i<CTR_FAN_CHANNEL_ALLNUM; i++)
	{
		value = atoi(_strtok_r(NULL, "-", &save));
		if((value <0) || (value >990))
		{
			err = 6;
			goto fnout;
		}
		
		dl1->fntp[i] = value;
	}
	

/************/
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "dlnqtp");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 7;
    goto fnout;
  }
	
	strncpy(ec_down_static.split_buf, ec_down_static.key->valuestring, 64);
	
	value = atoi(_strtok_r(ec_down_static.split_buf, "-", &save));
	if((value <0) || (value >990))
	{
		err = 7;
		goto fnout;
	}
	
	dl1->nqtp[0] = value;
	
	for(i=1; i<CTR_NUQ_CHANNEL_ALLNUM; i++)
	{
		value = atoi(_strtok_r(NULL, "-", &save));
		if((value <0) || (value >990))
		{
			err = 7;
			goto fnout;
		}
		
		dl1->nqtp[i] = value;
	}
	
	
#if (ROUTE_INTEGRATED > 0)
	Storage_SaveType sftype;

	if(memcmp(&Ctrl_ParaDL1, dl1, sizeof(Ctrl_ParaDL1Type)) != 0)
	{
		if(Ctr_CheckDL1Para(dl1) != 0)
		{
			err = 15;
			goto fnout;
		}
		
		Ctrl_ParaDL1 = *dl1;
		
		sftype = SFLASH_SAVE_CTRDL1;
		if(Storage_qhd != NULL)
		{
			xQueueSend(Storage_qhd, &sftype, 0);
		}
		
		Route_ec_tonet_dl1();
    DW_Send_ec_para_dl();
	}
#endif
	
fnout:
  cJSON_Delete(ec_down_static.key);
  cJSON_Delete(ec_down_static.params);
  cJSON_Delete(ec_down_static.root);
	
  vPortFree(proute);
  
  return err;
}

uint8_t Route_ec_down_dl2(const char *str, uint8_t len)
{
	uint8_t err = 0;
  Ctrl_ParaDL2Type *dl2 = NULL;
  int value;
	Route_PayloadType *proute = NULL;
  
  proute = pvPortMalloc(sizeof(Route_PayloadType));
  if(proute == NULL)
  {
    return 1;
  }
  
	memset(&ec_down_static, 0, sizeof(route_ec_down_static_type));
  memset(proute, 0, sizeof(Route_PayloadType));
	
	dl2 = (Ctrl_ParaDL2Type *)(proute->payload);
	
	
/* *** */ 	
  ec_down_static.root = cJSON_Parse(str);
  
  ec_down_static.params = cJSON_GetObjectItem(ec_down_static.root, "params");
	if(ec_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }
	

/* *** */  
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "zsb");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(ec_down_static.key->valuestring, NULL) != 0)
	{
		err = 3;
    goto fnout;
	}

#if (ROUTE_INTEGRATED == 0)
	strncpy(pl->did, key->valuestring, ROUTE_DID_BUFLEN);
#endif


/* *** */  
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "dlfnrt");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 4;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value <0) || ( value >20))
  {
    err = 4;
    goto fnout;
  }
	
	dl2->fnrttp = value;

	
/* *** */  
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "dlnqrt");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 5;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value <0) || ( value >20))
  {
    err = 5;
    goto fnout;
  }
	
	dl2->nqrttp = value;

	
/* *** */  
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "dlnqfq");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 5;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value != 0) && ( value != 1))
  {
    err = 5;
    goto fnout;
  }
	
	dl2->nqfq = value;


/* *** */  
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "dlslrt");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 5;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value <0) || ( value >20))
  {
    err = 5;
    goto fnout;
  }
	
	dl2->slrttp = value;
	
	
#if (ROUTE_INTEGRATED > 0)
	Storage_SaveType sftype;
	
	if(memcmp(&Ctrl_ParaDL2, dl2, sizeof(Ctrl_ParaDL2Type)) != 0)
	{
		if(Ctr_CheckDL2Para(dl2) != 0)
		{
			err = 6;
			goto fnout;
		}
		
		Ctrl_ParaDL2 = *dl2;
		
		sftype = SFLASH_SAVE_CTRDL2;
		if(Storage_qhd != NULL)
		{
			xQueueSend(Storage_qhd, &sftype, 0);
		}
		
		Route_ec_tonet_dl2();
    DW_Send_ec_para_dl();
	}
#endif
	
fnout:
  cJSON_Delete(ec_down_static.key);
  cJSON_Delete(ec_down_static.params);
  cJSON_Delete(ec_down_static.root);
	
  vPortFree(proute);
  
  return err;
}

uint8_t Route_ec_down_sk(const char *str, uint8_t len)
{
	uint8_t err = 0;
	uint8_t i;
	int value;
	char *save = NULL;
  Ctrl_ParaSKType *sk = NULL;
	Route_PayloadType *proute = NULL;
  
  proute = pvPortMalloc(sizeof(Route_PayloadType));
  if(proute == NULL)
  {
    return 1;
  }
  
	memset(&ec_down_static, 0, sizeof(route_ec_down_static_type));
  memset(proute, 0, sizeof(Route_PayloadType));
	
	sk = (Ctrl_ParaSKType *)(proute->payload);
	
	
/* *** */ 	
  ec_down_static.root = cJSON_Parse(str);
  
  ec_down_static.params = cJSON_GetObjectItem(ec_down_static.root, "params");
	if(ec_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }
	

/* *** */  
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "zsb");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(ec_down_static.key->valuestring, NULL) != 0)
	{
		err = 3;
    goto fnout;
	}

#if (ROUTE_INTEGRATED == 0)
	strncpy(pl->did, key->valuestring, ROUTE_DID_BUFLEN);
#endif

/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "skslkq");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 4;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value <0) || ( value >3600))
  {
    err = 4;
    goto fnout;
  }
	
	sk->slkq = value;


/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "skfntd");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 5;
    goto fnout;
  }
	
	if(strlen(ec_down_static.key->valuestring) != CTR_FAN_CHANNEL_ALLNUM)
	{
		err = 5;
    goto fnout;
	}
	
	for(i=0; i<CTR_FAN_CHANNEL_ALLNUM; i++)
	{
		if(ec_down_static.key->valuestring[i] == '0')
		{
			sk->fntd[i] = 0;
		}
		else if(ec_down_static.key->valuestring[i] == '1')
		{
			sk->fntd[i] = 1;
		}
		else
		{
			err = 5;
			goto fnout;
		}
	}

	
/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "sknqgb");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 6;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value <0) || ( value >3600))
  {
    err = 6;
    goto fnout;
  }
	
	sk->nqgb = value;
	

/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "skvftf");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 7;
    goto fnout;
  }
	
	if(strlen(ec_down_static.key->valuestring) != ((CTR_VFD_CHANNEL_ALLNUM * 2)-1))
	{
		err = 7;
    goto fnout;
	}
	
	for(i=0; i<CTR_VFD_CHANNEL_ALLNUM; i++)
	{
		if(ec_down_static.key->valuestring[2*i] == '0')
		{
			sk->vftf[i] = 0;
		}
		else if(ec_down_static.key->valuestring[2*i] == '1')
		{
			sk->vftf[i] = 1;
		}
		else
		{
			err = 7;
			goto fnout;
		}
	}


/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "skvfen");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 8;
    goto fnout;
  }
	
	if(ec_down_static.key->valuestring[0] == '0')
	{
		sk->vfen = 0;
	}
	else if(ec_down_static.key->valuestring[0] == '1')
	{
		sk->vfen = 1;
	}
	else
	{
		err = 8;
		goto fnout;
	}
	

/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "skvfjs");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 9;
    goto fnout;
  }
	
	strncpy(ec_down_static.split_buf, ec_down_static.key->valuestring, 64);
	
	value = atoi(_strtok_r(ec_down_static.split_buf, "-", &save));
	if((value <0) || (value >10))
	{
		err = 9;
		goto fnout;
	}
	
	sk->vfjs[0] = value;
	
	for(i=1; i<CTR_VFD_CHANNEL_ALLNUM; i++)
	{
		value = atoi(_strtok_r(NULL, "-", &save));
		if((value <0) || (value >10))
		{
			err = 9;
			goto fnout;
		}
		
		sk->vfjs[i] = value;
	}

	
/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "skfngb");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 10;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value <0) || ( value >3600))
  {
    err = 10;
    goto fnout;
  }
	
	sk->fngb = value;
	

/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "skslen");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 11;
    goto fnout;
  }
	
	if(ec_down_static.key->valuestring[0] == '0')
	{
		sk->slen = 0;
	}
	else if(ec_down_static.key->valuestring[0] == '1')
	{
		sk->slen = 1;
	}
	else
	{
		err = 11;
		goto fnout;
	}


/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "sknqen");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 12;
    goto fnout;
  }
	
	if(ec_down_static.key->valuestring[0] == '0')
	{
		sk->nqen = 0;
	}
	else if(ec_down_static.key->valuestring[0] == '1')
	{
		sk->nqen = 1;
	}
	else
	{
		err = 12;
		goto fnout;
	}

	
/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "sknqkq");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 13;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value <0) || ( value >3600))
  {
    err = 13;
    goto fnout;
  }
	
	sk->nqkq = value;
	

/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "sksltd");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 14;
    goto fnout;
  }
	
	if(strlen(ec_down_static.key->valuestring) != CTR_SHL_CHANNEL_ALLNUM)
	{
		err = 14;
    goto fnout;
	}
	
	for(i=0; i<CTR_SHL_CHANNEL_ALLNUM; i++)
	{
		if(ec_down_static.key->valuestring[i] == '0')
		{
			sk->sltd[i] = 0;
		}
		else if(ec_down_static.key->valuestring[i] == '1')
		{
			sk->sltd[i] = 1;
		}
		else
		{
			err = 14;
			goto fnout;
		}
	}


/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "skvfkq");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 15;
    goto fnout;
  }
	
	if(strlen(ec_down_static.key->valuestring) > 63)
	{
		err = 15;
    goto fnout;
	}
	
	strncpy(ec_down_static.split_buf, ec_down_static.key->valuestring, 64);
	
	value = atoi(_strtok_r(ec_down_static.split_buf, "-", &save));
	if((value <0) || (value >3600))
	{
		err = 15;
		goto fnout;
	}
	
	sk->vfkq[0] = value;
	
	for(i=1; i<CTR_VFD_CHANNEL_ALLNUM; i++)
	{
		value = atoi(_strtok_r(NULL, "-", &save));
		if((value <0) || (value >3600))
		{
			err = 15;
			goto fnout;
		}
		
		sk->vfkq[i] = value;
	}


/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "skfnkq");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 16;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value <0) || ( value >3600))
  {
    err = 16;
    goto fnout;
  }
	
	sk->fnkq = value;


/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "skfnen");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 17;
    goto fnout;
  }
	
	if(ec_down_static.key->valuestring[0] == '0')
	{
		sk->fnen = 0;
	}
	else if(ec_down_static.key->valuestring[0] == '1')
	{
		sk->fnen = 1;
	}
	else
	{
		err = 17;
		goto fnout;
	}


/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "skslgb");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 18;
    goto fnout;
  }
	
	value = atoi(ec_down_static.key->valuestring);
	if((value <0) || ( value >3600))
  {
    err = 18;
    goto fnout;
  }
	
	sk->slgb = value;

	
/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "skvfgb");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 19;
    goto fnout;
  }
	
	if(strlen(ec_down_static.key->valuestring) > 63)
	{
		err = 19;
    goto fnout;
	}
	
	strncpy(ec_down_static.split_buf, ec_down_static.key->valuestring, 64);
	
	value = atoi(_strtok_r(ec_down_static.split_buf, "-", &save));
	if((value <0) || (value >3600))
	{
		err = 19;
		goto fnout;
	}
	
	sk->vfgb[0] = value;
	
	for(i=1; i<CTR_VFD_CHANNEL_ALLNUM; i++)
	{
		value = atoi(_strtok_r(NULL, "-", &save));
		if((value <0) || (value >3600))
		{
			err = 19;
			goto fnout;
		}
		
		sk->vfgb[i] = value;
	}
	

	/* *** */
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "sknqtd");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 20;
    goto fnout;
  }
	
	if(strlen(ec_down_static.key->valuestring) != CTR_NUQ_CHANNEL_ALLNUM)
	{
		err = 20;
    goto fnout;
	}
	
	for(i=0; i<CTR_NUQ_CHANNEL_ALLNUM; i++)
	{
		if(ec_down_static.key->valuestring[i] == '0')
		{
			sk->nqtd[i] = 0;
		}
		else if(ec_down_static.key->valuestring[i] == '1')
		{
			sk->nqtd[i] = 1;
		}
		else
		{
			err = 20;
			goto fnout;
		}
	}


#if (ROUTE_INTEGRATED > 0)
	Storage_SaveType sftype;
	
	if(memcmp(&Ctrl_ParaSK, sk, sizeof(Ctrl_ParaSKType)) != 0)
	{
		if(Ctr_CheckSKPara(sk) != 0)
		{
			err = 15;
			goto fnout;
		}
		
		Ctrl_ParaSK = *sk;
		
		sftype = SFLASH_SAVE_CTRSK;
		if(Storage_qhd != NULL)
		{
			xQueueSend(Storage_qhd, &sftype, 0);
		}
		
		Route_ec_tonet_sk();
    DW_Send_ec_para_sk();
	}
#endif
	
fnout:
  cJSON_Delete(ec_down_static.key);
  cJSON_Delete(ec_down_static.params);
  cJSON_Delete(ec_down_static.root);
	
  vPortFree(proute);
  
  return err;
}

uint8_t Route_ec_down_td(const char *str, uint8_t len)
{
	Ctrl_ParaTDType *td = NULL;
	uint8_t i, zd_td;
	uint8_t err = 0;
  cJSON *array_item = NULL;
	Route_PayloadType *proute = NULL;
  
  proute = pvPortMalloc(sizeof(Route_PayloadType));
  if(proute == NULL)
  {
    return 1;
  }
  
	memset(&ec_down_static, 0, sizeof(route_ec_down_static_type));
  memset(proute, 0, sizeof(Route_PayloadType));
	
	td = (Ctrl_ParaTDType *)(proute->payload);
	
	
/* *** */ 	
  ec_down_static.root = cJSON_Parse(str);
  
  ec_down_static.params = cJSON_GetObjectItem(ec_down_static.root, "params");
	if(ec_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }
	

/* *** */  
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "zsb");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(ec_down_static.key->valuestring, NULL) != 0)
	{
		err = 3;
    goto fnout;
	}

#if (ROUTE_INTEGRATED == 0)
	strncpy(pl->did, key->valuestring, ROUTE_DID_BUFLEN);
#endif
	
/************/
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "td");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_Array))
  {
    err = 4;
    goto fnout;
  }
	
	if(cJSON_GetArraySize(ec_down_static.key) != 6)
  {
    err = 4;
    goto fnout;
  }

	/* 风机通道 */
	array_item = cJSON_GetArrayItem(ec_down_static.key, 0);
	if((array_item == NULL) || (array_item->type != cJSON_String))
	{
		err = 5;
		goto fnout;
	}
	
	if(strlen(array_item->valuestring) != 13)
	{
		err = 5;
		goto fnout;
	}
	
	for(i=0; i<13; i++)
	{
		if(array_item->valuestring[i] == '0')
		{
			td->fntd[i] = 0;
		}
		else if(array_item->valuestring[i] == '1')
		{
			td->fntd[i] = 1;
		}
		else
		{
			err = 5;
			goto fnout;
		}
	}
	
	/* 变频器通道 */
	array_item = cJSON_GetArrayItem(ec_down_static.key, 1);
	if((array_item == NULL) || (array_item->type != cJSON_String))
	{
		err = 6;
		goto fnout;
	}
	
	if(strlen(array_item->valuestring) != 4)
	{
		err = 6;
		goto fnout;
	}
	
	for(i=0; i<4; i++)
	{
		if(array_item->valuestring[i] == '0')
		{
			td->vftd[i] = 0;
		}
		else if(array_item->valuestring[i] == '1')
		{
			td->vftd[i] = 1;
		}
		else
		{
			err = 6;
			goto fnout;
		}
	}
	
  /* 水帘通道 */
	array_item = cJSON_GetArrayItem(ec_down_static.key, 2);
	if((array_item == NULL) || (array_item->type != cJSON_String))
	{
		err = 7;
		goto fnout;
	}
	
	if(strlen(array_item->valuestring) != 2)
	{
		err = 7;
		goto fnout;
	}
	
	for(i=0; i<2; i++)
	{
		if(array_item->valuestring[i] == '0')
		{
			td->sltd[i] = 0;
		}
		else if(array_item->valuestring[i] == '1')
		{
			td->sltd[i] = 1;
		}
		else
		{
			err = 7;
			goto fnout;
		}
	}
	
	/* 暖气通道 */
	array_item = cJSON_GetArrayItem(ec_down_static.key, 3);
	if((array_item == NULL) || (array_item->type != cJSON_String))
	{
		err = 8;
		goto fnout;
	}
	
	if(strlen(array_item->valuestring) != 3)
	{
		err = 8;
		goto fnout;
	}
	
	for(i=0; i<3; i++)
	{
		if(array_item->valuestring[i] == '0')
		{
			td->nqtd[i] = 0;
		}
		else if(array_item->valuestring[i] == '1')
		{
			td->nqtd[i] = 1;
		}
		else
		{
			err = 8;
			goto fnout;
		}
	}
	
	/* 报警通道 */
	array_item = cJSON_GetArrayItem(ec_down_static.key, 4);
	if((array_item == NULL) || (array_item->type != cJSON_String))
	{
		err = 9;
		goto fnout;
	}
	
	if(strlen(array_item->valuestring) != 1)
	{
		err = 9;
		goto fnout;
	}
	
	if(array_item->valuestring[0] == '0')
	{
		td->bjtd = 0;
	}
	else if(array_item->valuestring[0] == '1')
	{
		td->bjtd = 1;
	}
	else
	{
		err = 9;
		goto fnout;
	}
	
	/* 阻断通道 */
	array_item = cJSON_GetArrayItem(ec_down_static.key, 5);
	if((array_item == NULL) || (array_item->type != cJSON_String))
	{
		err = 10;
		goto fnout;
	}
	
	if(strlen(array_item->valuestring) != 1)
	{
		err = 10;
		goto fnout;
	}
  
	zd_td = Ctrl_ParaTD.zdtd;
  
	if(array_item->valuestring[0] == '0')
	{
		zd_td = 0;
	}
	else if(array_item->valuestring[0] == '1')
	{
		zd_td = 1;
	}
	else
	{
		err = 10;
		goto fnout;
	}

/***** 阻断通道权限标志, 只有当此标志位1时,阻断通道才能被改写 *******/  
	ec_down_static.key = cJSON_GetObjectItem(ec_down_static.params, "zd");
	if((ec_down_static.key == NULL) || (ec_down_static.key->type != cJSON_Number))
  {
    err = 11;
    goto fnout;
  }
	
	if(ec_down_static.key->valueint == 1)
	{
    td->zdtd = zd_td;
	}
  else
  {
    td->zdtd = Ctrl_ParaTD.zdtd;
  }
	
	
#if (ROUTE_INTEGRATED > 0)
	Storage_SaveType sftype;
	
	if(memcmp(&Ctrl_ParaTD, td, sizeof(Ctrl_ParaTDType)) != 0)
	{
		if(Ctr_CheckTDPara(td) != 0)
		{
			err = 12;
			goto fnout;
		}
		
		Ctrl_ParaTD = *td;
		
		sftype = SFLASH_SAVE_CTRTD;
		if(Storage_qhd != NULL)
		{
			xQueueSend(Storage_qhd, &sftype, 0);
		}
		
		Route_ec_tonet_td();
    DW_Send_ec_para_td();
	}
#endif

fnout:
	cJSON_Delete(array_item);
  cJSON_Delete(ec_down_static.key);
  cJSON_Delete(ec_down_static.params);
  cJSON_Delete(ec_down_static.root);
	
  vPortFree(proute);
  
  return err;
}
